<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Http\Controllers\Controller;

use DB;

use Crypt;

class Usercontroller extends Controller
{
    public function index(Request $request){
    	$data=DB::table('users')
    	-> where('username','like','%'.$request -> input('keyword').'%')
    	 -> paginate($request -> input('pages',10));

    	return view('admin.user.index',['data' => $data,'request' => $request -> all()]);
    }

    public function add(){
    	return view('admin.user.add');
    }

    public function insert(Request $request){
    	// var_dump($request -> all());
    	$data=$request -> except('_token','repassword');
    	// dd($data);
    	$this->validate($request, [
		    'username' => 'required|min:6|max:18',
		    'email' => 'required|email',
		    'password' => 'required|min:4|max:12',
		    'repassword' => 'required|same:password',
		    'Token' => 'required',
		    'status' => 'required',
		],[
			'username.required' => '用户名不能为空',
			'username.min' => '用户名不能少于6位',
			'username.max' => '用户名不能多于18位',
			'email.required' => '邮箱不能为空',
			'email.email' => '邮箱不符合要求',
			'password.required' => '密码不能为空',
			'password.min' => '密码不能少于4位',
			'password.max' => '密码不能多于12位',
			'repassword.required' => '确认密码不能为空',
			'status.required' => '状态不能为空',
			'Token.required' => '密码令牌不能为空'
		]);

		$data['password']=Crypt::encrypt($data['password']);

    	$res=DB::table('users') -> insert($data);

    	if ($res) {
    		return redirect('/admin/user');
    	}else{
    		return back()->with(['info' => '数据添加失败']);
    	}
    }

    public function ajaxUpdate(Request $request){
    	// 获取ID
    	$id = $request -> input('id');

    	// 查询数据
    	$data=DB::table('users') -> select('status') -> where('id',$id) -> first();

    	if($data -> status ==0){
    		DB::table('users') -> where('id',$id) -> update(['status' => 1]);
    		return response() -> json(1);
    	}else{
    		DB::table('users') -> where('id',$id) -> update(['status' => 0]);
    		return response() -> json(0);
    	}
    }

    public function ajaxUpdateUsername(Request $request){
    	// return '1111';
    	// dd($request -> all());
    	$res = DB::table('users') -> where('username',$request -> input('username')) -> first();

    	if($res){
    		// 书名用户名已经存在,返回标识符
    		return response() -> json(0);
    	}else{
    		$res1 = DB::table('users') -> where('id',$request -> input('id')) -> update(['username' => $request -> input('username')]);

    		if($res1){
    			return response() -> json(1);
    		}else{
    			return response() -> json(2);
    		}
    	}
    }

    public function edit($id){
        // dd($id);
        $data = DB::table('users') -> where('id' , $id) -> first();
        // dd($data);
        return view('admin.user.edit',['data' => $data]);
    }


    public function doedit(Request $request){
        // var_dump($request -> all());
        $id =$request -> id;
        $data=$request -> except('_token','id');
        // dd($data);
        $this->validate($request, [
            'username' => 'required|min:6|max:18',
            'email' => 'required|email',
            'password' => 'required|min:4|max:12',
            'status' => 'required',
        ],[
            'username.required' => '用户名不能为空',
            'username.min' => '用户名不能少于6位',
            'username.max' => '用户名不能多于18位',
            'email.required' => '邮箱不能为空',
            'email.email' => '邮箱不符合要求',
            'password.required' => '密码不能为空',
            'password.min' => '密码不能少于4位',
            'password.max' => '密码不能多于12位',
            'status.required' => '状态不能为空',
            'Token.required' => '密码令牌不能为空'
        ]);

        $data['password']=Crypt::encrypt($data['password']);

        // dd($data);

        $res=DB::table('users') ->where('id',$id) -> update($data);

        if ($res) {
            return redirect('/admin/user');
        }else{
            return back()->with(['info' => '数据修改失败']);
        }
    }

    public function delete($id){
        $res = DB::table('users')->where('id',$id)->delete();

        if($res){
            return redirect('/admin/user');
        }else{
            return back()->with(['info' => '数据修改失败']);
        }
    }
}
